package cn.iocoder.yudao.module.scrm.controller.admin.schedules;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesVisitorPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesVisitorSaveReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesVisitorRespVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.schedulesvisitor.SchedulesVisitorDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.schedules.SchedulesMapper;
import cn.iocoder.yudao.module.scrm.service.schedules.SchedulesVisitorService;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Tag(name = "管理后台 - 排期访客")
@RestController
@RequestMapping("/scrm/schedules-visitor")
@Validated
public class SchedulesVisitorController {

    @Resource
    private SchedulesVisitorService schedulesVisitorService;

    @Resource
    private SchedulesMapper schedulesMapper;

    @Resource
    private PermissionApi permissionApi;

    @PostMapping("/create")
    @Operation(summary = "创建排期访客")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-visitor:create')")
    public CommonResult<Long> createSchedulesVisitor(@Valid @RequestBody SchedulesVisitorSaveReqVO createReqVO) {
        return success(schedulesVisitorService.createSchedulesVisitor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排期访客")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-visitor:update')")
    public CommonResult<Boolean> updateSchedulesVisitor(@Valid @RequestBody SchedulesVisitorSaveReqVO updateReqVO) {
        schedulesVisitorService.updateSchedulesVisitor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排期访客")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:schedules-visitor:delete')")
    public CommonResult<Boolean> deleteSchedulesVisitor(@RequestParam("id") Long id) {
        schedulesVisitorService.deleteSchedulesVisitor(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排期访客")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-visitor:query')")
    public CommonResult<SchedulesVisitorRespVO> getSchedulesVisitor(@RequestParam("id") Long id) {
        SchedulesVisitorDO schedulesVisitor = schedulesVisitorService.getSchedulesVisitor(id);
        return success(BeanUtils.toBean(schedulesVisitor, SchedulesVisitorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排期访客分页")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-visitor:query')")
    public CommonResult<PageResult<SchedulesVisitorRespVO>> getSchedulesVisitorPage(@Valid SchedulesVisitorPageReqVO pageReqVO) {
        //不是超级管理员只能查询自己的
        Long loginUserId = getLoginUserId();
        if (!permissionApi.roleIsSuperAdmin(loginUserId).getCheckedData()){
            pageReqVO.setCreator(Objects.requireNonNull(loginUserId).toString());
        }

        PageResult<SchedulesVisitorDO> pageResult = schedulesVisitorService.getSchedulesVisitorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SchedulesVisitorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排期访客 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-visitor:export')")
    @OperateLog(type = EXPORT)
    public void exportSchedulesVisitorExcel(@Valid SchedulesVisitorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SchedulesVisitorDO> list = schedulesVisitorService.getSchedulesVisitorPage(pageReqVO).getList();
        List<SchedulesVisitorRespVO> beanVo = BeanUtils.toBean(list, SchedulesVisitorRespVO.class);
        beanVo.forEach(item-> item.setSchedulesIdNmame(schedulesMapper.selectById(item.getSchedulesId()).getName()));
        //设置排期名称
        // 导出 Excel
        ExcelUtils.write(response, "排期访客.xls", "数据", SchedulesVisitorRespVO.class,
                beanVo);
    }

}